package com.kichina.galaxy.dev.tools.aliyun.model;

import com.alibaba.fastjson.JSON;
import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.CreateThingModelRequest;
import com.aliyun.iot20180120.models.CreateThingModelResponse;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.kichina.galaxy.dev.tools.aliyun.model.bean.AliyunModelProperties;
import com.kichina.galaxy.dev.tools.aliyun.model.bean.ThingModelJsonModel;
import com.google.common.collect.Lists;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Surging
 * @version 1.0
 * @ClassName AliyunThingModelUtil
 * @date 2022/6/8 22:22
 * @desc aliyun物联网平台给产品添加物模型测试类
 */
public class Sample {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "iot.cn-beijing.aliyuncs.com";
        return new Client(config);
    }

    public static void main(String[] args_) throws Exception {

        Client client = Sample.createClient("LTAI4FffY9AVJCCirHyBHfTu", "mKzXPRI3HAGQ7p3JQK17vUxuWKiNZv");
        ThingModelJsonModel thingModelJsonModel = new ThingModelJsonModel();
        List<AliyunModelProperties> aliyunModelProperties = Lists.newArrayList();
        /**
         * {
         * "properties":[
         * {
         * "identifier":"SimCardType",
         * "dataSpecs":{
         * "max":"1",
         * "dataType":"INT",
         * "unit":"mmHg",
         * "min":"0",
         * "step":"1"
         * },
         * "std":false,
         * "custom":true,
         * "dataType":"INT",
         * "rwFlag":"READ_ONLY",
         * "productKey":"gd83Jw4TkT8",
         * "required":false,
         * "customFlag":true,
         * "name":"sim卡类型"
         * }
         * ]
         * }
         */

        AliyunModelProperties aliyng = new AliyunModelProperties();
        Map<String, Object> dataSpecsMap = new HashMap<>();
        aliyng.setCustomFlag(true);
        aliyng.setProductKey("gd83Jw4TkT8");
        aliyng.setDataType("INT");
        aliyng.setIdentifier("SimCardType1");
        aliyng.setRequired(false);
        aliyng.setRwFlag("READ_WRITE");
        aliyng.setName("sim卡类型1");
        dataSpecsMap.put("dataType","INT");
        aliyng.setDataSpecs(dataSpecsMap);
        aliyunModelProperties.add(aliyng);
        thingModelJsonModel.setProperties(aliyunModelProperties);
        String jsonString = JSON.toJSONString(thingModelJsonModel);
        System.out.println(jsonString);
        CreateThingModelRequest createThingModelRequest = new CreateThingModelRequest()
                .setIotInstanceId("iot-010a08z4")
                .setProductKey("gd83Jw4TkT8")
                .setThingModelJson(jsonString);
//                .setThingModelJson("{\"properties\":[{\"identifier\": \"SimCardType\",\"dataSpecs\": {\"max\": \"1\", \"dataType\": \"INT\",\"unit\": \"mmHg\",\"min\": \"0\",\"step\": \"1\"},\"std\": false,\"custom\": true,\"dataType\": \"INT\",\"rwFlag\": \"READ_ONLY\",\"productKey\": \"gd83Jw4TkT8\",\"required\": false,\"customFlag\": true, \"name\": \"sim卡类型\"}]}");
        RuntimeOptions runtime = new RuntimeOptions();
        CreateThingModelResponse resp = client.createThingModelWithOptions(createThingModelRequest, runtime);
        com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
    }
}
